// 页面路径：store/index.js
import Vue from "vue";
import Vuex from "vuex";
Vue.use(Vuex);
import {
	ACSSESS_USER,
	ACCESS_TOKEN
} from "@/common/utils/constants.js";
import tool from '@/common/utils/tool.js';

const store = new Vuex.Store({
	state: {
		hasLogin: false,
		token: '',
		user: {},
	},
	getters: {

	},
	mutations: {
		login(state) {
			state.hasLogin = true;
		},
		logout(state) {
			state.hasLogin = false;
		},
		setToken(state, payload) {
			state.token = payload.token;
		},
		setUser(state, payload) {
			state.user = payload.user;
		}
	},
	actions: {
		async checkLoginAction({
			commit,
			dispatch,
			state
		}, payload) {
			return new Promise(async function(resolve, reject) {
				if (state.hasLogin === false) {
					var token = await uni.getStorageSync(ACCESS_TOKEN);
					if (token) {
						commit({ type: 'setToken', token: token } );
						commit({ type: 'login' });
					} else {
						commit({ type: 'setToken', token: '' } );
						commit({ type: 'logout'});
						tool.goToPage('/pages/Login/index','','reLaunch');
					}
				}
				resolve(true);
			});
		},
	},
});

const getToken = function(payload) {
	return new Promise(async function(resolve, reject) {});
};
export default store;